#include "BitmapCharacterSet.h"
#include "Ulti.h"

CharacterSet::CharacterSet(const char* fname)
{
	BuildFont(fname);
}


CharacterSet* CharacterSet::Instance()
{
	static CharacterSet _instance;
	return &_instance;
}

int CharacterSet::BuildFont(const char* fname)
{
	FILE* ifp;
	ifp = fopen(fname,"r");
	if(ifp == NULL)
	{
		printf("can't open file format\n");
		return 1;
	}
	
	CharacterSet* CharSet = CharacterSet::Instance();
	//Init CharSet
	{
	getNextAtribute("size=",ifp,CharSet->Size);
	getNextAtribute("lineHeight=",ifp,CharSet->LineHeight);
	getNextAtribute("base=",ifp,CharSet->Base);
	getNextAtribute("scaleW=",ifp,CharSet->BMWidth);
	getNextAtribute("scaleH=",ifp,CharSet->BMHeight);
	getNextPath("file=",ifp,CharSet->BitmapPath);
	}
	int i=0;
	for(;findOnward("char",ifp)!=EOF;i++)
	{
		getNextBYTEAtribute("id=",ifp,CharSet->ch[i].id);
		getNextBYTEAtribute("x=",ifp,CharSet->ch[i].x);
		getNextBYTEAtribute("y=",ifp,CharSet->ch[i].y);
		getNextBYTEAtribute("width=",ifp,CharSet->ch[i].width);
		getNextBYTEAtribute("height=",ifp,CharSet->ch[i].height);
		getNextBYTEAtribute("xadvance=",ifp,CharSet->ch[i].xadvance);
	}
	
//	CharSet.BitmapFontIMG = al_load_bitmap(CharSet.BitmapPath);
//	al_convert_mask_to_alpha(CharSet.BitmapFontIMG,al_map_rgb(0,0,0));

	fclose(ifp);
	return 0;
}